<template>
  <div>
    <el-table v-loading="loading" :data="tableData" style="width: 100%" @sort-change="sortTable">
      <el-table-column prop="lobbyTypeName" label="影厅类型" sortable></el-table-column>
      <el-table-column prop="cinemaName" label="影院"></el-table-column>
      <el-table-column prop="lobbyName" label="影厅名"></el-table-column>
      <el-table-column prop="introduce" label="影厅描述"></el-table-column>
      <el-table-column prop="col" label="列数" sortable></el-table-column>
      <el-table-column prop="rowNum" label="行数" sortable></el-table-column>
      <el-table-column prop="pic" label="影厅图片">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" :preview-src-list="[scope.row.pic]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="insertTime" label="添加时间" sortable></el-table-column>
      <el-table-column prop="updateTime" label="修改时间" sortable></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEditSeat(scope)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import router from "@/router";
import {mapMutations} from "vuex";

export default {
  data() {
    return {
      loading: true,
      ChangeLoading:true,
      tableData: [],
      tableDateCopy: [],
    };

  },
  methods: {
    ...mapMutations(['changeX','changeY','changeReset']),

    // 编辑座位
    handleEditSeat(scope){
      this.changeX(scope.row.col)
      this.changeY(scope.row.rowNum)

      router.push({
        name: 'seat',
        params: {
          id: scope.row.id,
        }
      })
    },
    // 删除
    handleDelete(scope) {
      this.$confirm('确定删除嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('/manage/lobby/delete', {
          params: {
            id: scope.row.id
          }
        }).then(response => {
          if (response.data.code == 200){
            // 丛数组中删除
            this.tableData.splice(scope.$index, 1)
            this.tableDateCopy = [...this.tableData]
            this.$message.success('删除成功');
          }else {
            this.$message.error('删除失败');
          }
        }).catch(error => {
          console.log(error)
          this.$message.error('服务异常，请联系管理员或稍后再试');
        })
      }).catch(() => {});
    },
    // 列表排序
    sortTable({prop, order}) {
      // prop: 排序的字段
      // order: 排序的顺序，ascending/descending/null
      if (order === "ascending") {
        // 升序排序
        this.tableData.sort((a, b) => this.sortName(a[prop], b[prop]));
      } else if (order === "descending") {
        // 降序排序
        this.tableData.sort((a, b) => this.sortName(b[prop], a[prop]));
      } else {
        // 不排序，恢复原始顺序
        this.tableData = [...this.tableDateCopy];
      }
    },
    // 排序
    sortName(a, b) {
      if (a < b) return -1;
      if (a > b) return 1;
      return 0;
    },
    // 获取影厅列表
    getLobbyTypeList() {
      axios.get('/manage/lobby/lobbyInfo').then(response => {
        let list = response.data.data;
        list.forEach(item => this.tableData.push(item))
        // 拷贝 tableDate 到 tableDateCopy
        this.tableDateCopy = [...this.tableData];
      }).catch(error => {
      }).finally(() => this.loading = false)
    }
  },
  created() {
    // 获取影厅信息
    this.getLobbyTypeList();
  }
}
</script>

<style scoped>

</style>